//
//  BaseTableViewController.m
//  YiCuiMiaoZhang
//
//  Created by Sings on 2019/5/22.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "BaseTableViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "Masonry.h"

@interface BaseTableViewController ()

@property (nonatomic, strong) BaseTableControllerModel *viewModel;

@end

@implementation BaseTableViewController

@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initTableView];
    [self initRefresh];
}
- (void)initRefresh {

    BaseWeakObj(self) if (self.viewModel.footerRefresh) {
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakself.viewModel refreshTableFooterEnd:^{
                if ([weakself.tableView.mj_footer isRefreshing]) {

                    [weakself.tableView.mj_footer endRefreshing];
                }
            }];
        }];
    }

    if (self.viewModel.headerRefresh) {
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself.viewModel refreshTableHeaderEnd:^{
                if ([weakself.tableView.mj_header isRefreshing]) {

                    [weakself.tableView.mj_header endRefreshing];
                }
            }];
        }];
    }
}

- (void)initTableView {
//    self.view.backgroundColor              = [UIColor whiteColor];
    BZTHEME_COLOR(skin_content_bg_color,self.view,backgroundColor);
    BaseTableView *tableView             = [[BaseTableView alloc] init];
    tableView.separatorStyle               = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.delegate                     = self;
    tableView.dataSource                   = self;
//    tableView.backgroundColor              = [UIColor groupTableViewBackgroundColor];
    BZTHEME_COLOR(skin_page_bg_color,tableView,backgroundColor);
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    self.tableView = tableView;
}

#pragma mark tableVeiw代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BaseTableSectionModel *sectionModel = self.viewModel.sectionArray[section];
    __weak typeof(self) weakSelf = self;
    sectionModel.sectionRefresh = ^(UITableViewRowAnimation animation) {
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
        [weakSelf.tableView reloadSections:indexSet withRowAnimation:animation];
    };
    sectionModel.tableRefresh = ^(UITableViewRowAnimation animation) {
        [weakSelf.tableView reloadData];
    };
    return self.viewModel.sectionArray[section].rowArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseViewModel *viewModel =
        self.viewModel.sectionArray[indexPath.section].rowArray[indexPath.row];
    BaseTableViewCell *cell = [[[MZRouting sharedInstance] getTableCellClassWithViewModel:viewModel]
        basetabelViewCellWithTableView:(BaseTableView *)tableView];
    cell.viewModel = viewModel;
    if (((BaseTableSectionModel *)(self.viewModel.sectionArray[indexPath.section])).isMove) {
        [self cellViewAddPan:cell];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.viewModel.sectionArray[indexPath.section].rowArray[indexPath.row].height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.viewModel.sectionArray[section].sectionHeaderView.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.viewModel.sectionArray[section].sectionFooterView.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BaseViewModel *viewModel = self.viewModel.sectionArray[section].sectionHeaderView;
    BaseTableViewHeaderFooterView *headerV =
        [[[MZRouting sharedInstance] getTableHeaderFooterWithViewModel:viewModel]
            baseTableViewHeaderFooterViewWithTableView:(BaseTableView *)tableView];
    headerV.viewModel = viewModel;
    return headerV;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    BaseViewModel *viewModel = self.viewModel.sectionArray[section].sectionFooterView;
    BaseTableViewHeaderFooterView *footerV =
        [[[MZRouting sharedInstance] getTableHeaderFooterWithViewModel:viewModel]
            baseTableViewHeaderFooterViewWithTableView:(BaseTableView *)tableView];
    footerV.viewModel = viewModel;
    return footerV;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
                  editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseCellModel *viewModel =
        self.viewModel.sectionArray[indexPath.section].rowArray[indexPath.row];
    return [viewModel.actionArray tableCellAction];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BaseCellModel *viewModel =
        self.viewModel.sectionArray[indexPath.section].rowArray[indexPath.row];
    if (viewModel.cellClick) { viewModel.cellClick(indexPath); }
}

- (void)cellDataOldIndex {
    if (self.oldPath != nil && self.path != nil && (self.oldPath.row != self.path.row)) {
        [self.viewModel oldPath:self.oldPath MoveToPath:self.path];
    }
}

- (void)refreshView {
    [self.tableView reloadData];
}

@end
